﻿using System;
using System.Collections.Generic;
using IrrlichtLime;
using IrrlichtLime.Core;
using Timed;
using IrrlichtLime.Scene;
using IrrlichtLime.Video;
using Iso.Common;
using Iso;
using Iso.Core;

namespace Game.Plugin
{
    public class OverText : ICustomPlugin
    {
        const int texwidth = 160, texheight = 80;
        const string font = "Helvetica";
        const int fontSize = 30;
        System.Drawing.Color color = System.Drawing.Color.Red;
        const float width = 12f, height = 11f, offsetY = 10f;
        const uint duration = 2000;

        public string name, value;
        public Vector3Df position, originalPosition;
        public MeshSceneNode sceneNode;

        public OverText(string name, string value, Vector3Df position)
        {
            this.name = name;
            this.value = value;
            this.originalPosition = position;
            this.position = position + new Vector3Df(0, offsetY, 0);
        }

        public void Load()
        {            
            Texture tex = value.RenderText(texwidth, texheight, new System.Drawing.Font(font, fontSize), new System.Drawing.SolidBrush(color), System.Drawing.StringAlignment.Center);

            float halfWidth = width / 2;
            MeshBuffer buff = MeshBuffer.Create(VertexType.Standard, IndexType._32Bit);
            buff.Append(new Vertex3D[]{
                new Vertex3D(new Vector3Df(-halfWidth, 0, 0), new Vector3Df(0, 0, 0), Color.OpaqueWhite, new Vector2Df(0,1)),
                new Vertex3D(new Vector3Df(halfWidth, 0, 0), new Vector3Df(0, 0, 0), Color.OpaqueWhite, new Vector2Df(1,1)),
                new Vertex3D(new Vector3Df(-halfWidth, height, 0), new Vector3Df(0, 0, 0), Color.OpaqueWhite, new Vector2Df(0,0)),
                new Vertex3D(new Vector3Df(halfWidth, height, 0), new Vector3Df(0, 0, 0), Color.OpaqueWhite, new Vector2Df(1,0))},
                new uint[] { 1, 0, 2, 1, 2, 3 });
            buff.RecalculateBoundingBox();
            Mesh mesh = Mesh.Create();
            mesh.AddMeshBuffer(buff);
            buff.Drop();
            mesh.RecalculateBoundingBox();

            sceneNode = Engine.device.SceneManager.AddMeshSceneNode(mesh, null, -1, position);
            sceneNode.SetMaterialTexture(0, tex);
            sceneNode.SetMaterialFlag(MaterialFlag.Lighting, false);
            sceneNode.SetMaterialType(MaterialType.TransparentAlphaChannelRef);
            sceneNode.SetMaterialFlag(MaterialFlag.BackFaceCulling, false);
            sceneNode.Rotation = new Vector3Df(0, ((Scene)Engine.currentScene).spriteRotation, 0);

            Engine.timeline.operations.Add(new LerpOperation(Engine.currentTime, Engine.currentTime + duration,
                new EventHandler<LerpEventArgs>((object s, LerpEventArgs args) =>
                    {
                        sceneNode.Position = Utility.Lerp(position, originalPosition, (float)(args.current - args.begin) / (float)(args.end - args.begin));
                    })));

            Engine.timeline.operations.Add(new TimedOperation(Engine.currentTime + duration + 10,
                new EventHandler<TimedEventArgs>((object s, TimedEventArgs args) =>
                {
                    Unload();
                    ((Scene)Engine.currentScene).plugins.Remove(name);
                })));
        }

        public void Unload()
        {
            sceneNode.Remove();
        }

        public void OnEvent(Event evnt)
        {

        }

        public void OnUpdate()
        {

        }

        public void OnDraw()
        {

        }

        public PluginDrawOrder GetDrawOrder()
        {
            return PluginDrawOrder.Off;
        }
    }
}
